/**
 * 拜访内容设置维护 编辑页面
 * @author mengyb
 * @date 2019/04/10
 */
define(function () {
    var self,
        app=win.app,
        doc=win.doc,
        vueData={
            isAdd:false,
            isDetail:false,
            form:'',
            rules:{
                GWBF001_NAME:[
                    {required:true,message:'不可为空',trigger:'change'}
                ],
                GWBF002:[
                    {required:true,message:'不可为空',trigger:'blur'}
                ],
                GWBFL004:[
                    {required:true,message:'不可为空',trigger:'blur'}
                ],
                GWBFL005:[
                    {required:true,message:'不可为空',trigger:'blur'}
                ],
                GWBFACTI:[
                    {required:true,message:'不可为空',trigger:'change'}
                ]
            },
            selectOptions:{
                sys_y_n:app.datas_("sys_y_n")
            }
        },
        orgsList=[],
        pageVue,
        callobj,
        layOpenIndex,
        moduleName
    ;


    var Gwai235Edit=app.Module.extend({

        //初始化
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai235_edit',
                _loadTplsFull: [
                    {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                    }
                ]
            }, win);
        },

        //开窗后回调函数
        _call: function (tagData) {
            pageVue.$refs['form'].resetFields();
            moduleName=tagData.moduleName;
            layOpenIndex=tagData.layOpenIndex;

            if(tagData.is_detail){
                vueData.isDetail = true;
            }else {
                vueData.isDetail = false;
            }
            if (tagData.isAdd){
                vueData.isAdd=true;
                app.extendUpLow({
                    target:vueData.form,
                    res:{
                        GWBF001_NAME:'',
                        GWBF002:'',
                        GWBFL005:'',
                        GWBFL004:'',
                        GWBFACTI:'Y'
                    },
                    reset:true
                })
                ;
            }else {
                vueData.isAdd=false;
                callobj=tagData.obj;
                app.extendUpLow({
                    target:vueData.form,
                    res: callobj.data,
                    reset:true
                });
            }
        },

        //新增
        add:function(){
            app.ajax({
                xurl:'base/gwai235/add',
                xdata:vueData.form,
                loading:true,
                okmsg:moduleName+'信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule("gwai235").reloadTableCurr();
                        layer.close(layOpenIndex);
                    }

                }
            })
        },

        //修改
        edit: function () {
            app.ajax({
                xurl:'base/gwai235/edit',
                xdata:vueData.form,
                loading:true,
                okmsg: moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        callobj.update(result.xdata);
                        layer.close(layOpenIndex);
                    }
                }
            })
        },

        //DOM加载完成后调用
        _ready: function () {

            vueData.form=app.fields_(['base_gwai235']);

            //实例化vue
            pageVue = new Vue({
                el:'#gwai235_edit',
                data:vueData,
                methods:{
                    //关闭窗口
                    cancle: function () {
                        layer.close(layOpenIndex);
                    },
                    //保存
                    save: function () {
                        //表单校验
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if(vueData.isAdd){
                                    self.add();
                                }else{
                                    self.edit();
                                }
                            }

                        });
                    },
                    //检查组织编号是否存在
                    check:function(){
                      if(vueData.form.GWBF001_NAME != ''){
                          var obj={};
                          obj = orgsList.find(function (item) {
                              return item.NAME === vueData.form.GWBF001_NAME;
                          });
                          if(typeof obj == "undefined"){
                              vueData.form.GWBF001_NAME='';
                              vueData.form.GWBF001='';
                          }else{
                              vueData.form.GWBF001=item.ID;
                          }
                      }else{
                          vueData.form.GWBF001='';
                      }
                    },


                    //组织开窗
                    openOrg: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_sysorg_id',
                                    columnList: [{label: '组织编号', prop: 'ID', width: '150'},
                                        {label: '组织名称', prop: 'NAME', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWBF001 = data.ID;
                                    vueData.form.GWBF001_NAME = data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择服务组织',
                                area: ['600px', '470px']
                            }
                        });
                    }
                },
                //使用ElementUI初始化DOM
                created: function () {
                    self._setEleIsReady('gwai235_edit');
                }
            })

        },

        _lazy: function () {
            app.ajax({
                xurl:'base/gwai235/getOrgsList',
                cb: function (result) {
                    orgsList=result.data.list;
                }
            })
            this._super();
        }

    });



    return new Gwai235Edit();

});